//
//  HFCollectionViewOne.m
//  ThridPackage
//
//  Created by 韩艳锋 on 2019/7/29.
//  Copyright © 2019 韩艳锋. All rights reserved.
//

#import "HFCollectionViewOne.h"

@interface HFCollectionViewOne ()<UICollectionViewDelegate, UICollectionViewDataSource, HFRowDelegate>

@property (strong, nonatomic) NSArray<HFRow*> *sourceArray;

@property (strong, nonatomic) NSMutableArray<NSString*> *hasRegist;
@end

@implementation HFCollectionViewOne

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.dataSource = self;
        self.delegate = self;
        self.hasRegist = [NSMutableArray array];
    }
    return self;
}

- (void)setSource:(NSArray<HFRow*> *)sourceArray {
    self.sourceArray = sourceArray;
    [self reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HFRow * row = self.sourceArray[indexPath.row];
    row.delegate = self;

    return [row cellWithCollectionView:collectionView indexPath:indexPath];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.sourceArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.actionBack) {
        self.actionBack(self.sourceArray[indexPath.row], -1, indexPath, nil);
    }
}

- (void)actionFromCellRow:(HFRow *)viewData tag:(NSInteger)tag value:(id)value {
    NSIndexPath * indexPath = nil;
    NSArray * array = [self.sourceArray copy];
    for (int i = 0; i != array.count; i++) {
        if ([array[i] isEqual:viewData]) {
            indexPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }
    if (self.actionBack) {
        self.actionBack(viewData, tag, indexPath, value);
    }
}
@end

